﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Medical.Consts;
using Sector7.Medical.EntityType;
using Sector7.Medical.Enums;
using Sector7.Medical.Biz;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Medical.STOM.Controls
{
    /// <summary>
    /// DrugItemControlForADJ.xaml 的交互逻辑
    /// </summary>
    public partial class DrugItemControlForADJ : UserControl
    {
        #region Accessor
        /// <summary>
        /// 点击添加事件
        /// </summary>
        public event EventHandler ClickAdd = null;

        /// <summary>
        /// 点击删除事件
        /// </summary>
        public event EventHandler ClickDelete = null;

        /// <summary>
        /// 药品项改变事件
        /// </summary>
        public event EventHandler ItemUpdated = null;
        #endregion

        #region Public functions
        /// <summary>
        /// 设置价格涨幅
        /// </summary>
        /// <param name="rangeOfPurchases"></param>
        /// <param name="rangeOfSales"></param>
        public void SetPriceRange(decimal rangeOfPurchases, decimal rangeOfSales)
        {
            m_rangeOfPurchases = rangeOfPurchases;
            m_rangeOfSales = rangeOfSales;
        }

        /// <summary>
        /// 获取控件相关采购单项信息
        /// </summary>
        /// <param name="recordId"></param>
        /// <returns></returns>
        public M7_AdjustRecordItem GetItem(long recordId)
        {
            M7_AdjustRecordItem item = null;
            try
            {
                item = new M7_AdjustRecordItem();
                var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
                item.RecordId = recordId;
                item.DrugId = selectedDrug.DrugId;
                item.ItemId = m_uniqueId;

                var spec = specControl.GetSpec();
                item.SpecId = spec.SpecId;
                //item.UnitId = selectedDrug.Price.UnitId;
                //item.OldPurchasesPrice = selectedDrug.Price.PurchasesPrice;
                //item.OldSalesPrice = selectedDrug.Price.SalesPrice;
                item.NewPurchasesPrice = Convert.ToDecimal(txbNewPrice_P.Text);
                item.NewSalesPrice = Convert.ToDecimal(txbNewPrice_S.Text);
            }
            catch (Exception)
            {
                item = null;
            }

            return item;
        }
        #endregion

        #region Private functions
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            m_uniqueId = DateTime.Now.Ticks;

            cbbDrugList.TemplateType = DrugTemplateType.AdjustRecord;
        }

        private void OnClickAdd(EventArgs e)
        {
            if (ClickAdd != null)
            {
                ClickAdd(this, e);
            }
        }

        private void OnClickDelete(EventArgs e)
        {
            if (ClickDelete != null)
            {
                ClickDelete(this, e);
            }
        }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            OnClickAdd(new EventArgs());
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            OnClickDelete(new EventArgs());
        }

        private void cbbDrugList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            var selectedDrug = cbbDrugList.SelectedItem as M7_Drug;
            if (selectedDrug != null)
            {
                lblDrugId.Content = selectedDrug.DrugId;
                lblProductName.Content = selectedDrug.ProductName;
                lblSpec.Content = string.Format("{0} {1} / {2}",
                    selectedDrug.Spec,
                    selectedDrug.SpecUnit.UnitName,
                    selectedDrug.UsageUnit.UnitName);
                //specControl.SetSpec(selectedDrug.Price.Spec);
                //lblPriceUnit.Content = selectedDrug.Price.Unit.UnitName;
                //lblCurPrice_P.Content = selectedDrug.Price.PurchasesPrice;
                //lblCurPrice_S.Content = selectedDrug.Price.SalesPrice;

                //txbNewPrice_P.Text = (selectedDrug.Price.PurchasesPrice * (1 + m_rangeOfPurchases)).ToString();
                //txbNewPrice_S.Text = (selectedDrug.Price.SalesPrice * (1 + m_rangeOfSales)).ToString();
            }
        }

        private void txbNewPrice_TextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox txbNewPrice = sender as TextBox;
            int tag = Convert.ToInt32(txbNewPrice.Tag);

            try
            {
                Convert.ToDecimal(txbNewPrice.Text);
            }
            catch
            {
                Alert.ShowDialog(AlertType.Error, "请输入正确的数字设置价格！");
                txbNewPrice.SelectAll();
            }
        }
        #endregion

        #region Structure
        public DrugItemControlForADJ()
        {
            InitializeComponent();
        }
        #endregion

        #region Inner members
        private long m_uniqueId = -1;
        private decimal m_rangeOfPurchases = 0;
        private decimal m_rangeOfSales = 0;
        #endregion
    }
}
